<?php
namespace app\restaurant\model;

use app\BaseModel;
use think\facade\Db;

class Goods extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_restaurant_goods';


    protected $spe_price_model;

    protected $cate_model;


    protected $append = [

      'feeding',

      'show_price',

      'total_stock',

      'show_member_price',

      'cate_text',

      'show_original_price',

      'show_sale_num'

    ];


    /**
     * @author chenniang
     * @DataTime: 2020-10-26 16:52
     * @功能说明:展示价格
     */
    public function getShowSaleNumAttr($value,$data){

        if(isset($data['sale_num'])&&isset($data['true_sale_num'])){

            return $data['sale_num']+$data['true_sale_num'];
        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-10 13:33
     * @功能说明:原价
     */
    public function getShowOriginalPriceAttr($value,$data){

        if(!empty($data['id'])){

            if(empty($this->spe_price_model)){

                $this->spe_price_model =  new SpePrice();
            }

            $arr = $this->spe_price_model->where(['goods_id'=>$data['id'],'status'=>1])->min('original_price');

            return round($arr,2);

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-10 13:33
     * @功能说明:售价
     */
    public function getShowPriceAttr($value,$data){

        if(!empty($data['id'])){

            if(empty($this->spe_price_model)){

                $this->spe_price_model =  new SpePrice();
            }

            $arr = $this->spe_price_model->where(['goods_id'=>$data['id'],'status'=>1])->min('price');

            return round($arr,2);

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-10 13:33
     * @功能说明:会员价
     */
    public function getShowMemberPriceAttr($value,$data){

        if(!empty($data['id'])){

            if(empty($this->spe_price_model)){

                $this->spe_price_model =  new SpePrice();
            }

            $arr = $this->spe_price_model->where(['goods_id'=>$data['id'],'status'=>1])->min('member_price');

            return round($arr,2);

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-10 13:33
     * @功能说明:售价
     */
    public function getTotalStockAttr($value,$data){

        if(!empty($data['id'])){

            if(empty($this->spe_price_model)){

                $this->spe_price_model =  new SpePrice();
            }

            $arr = $this->spe_price_model->where(['goods_id'=>$data['id'],'status'=>1])->sum('stock');

            return $arr;

        }

    }



    /**
     * @author chenniang
     * @DataTime: 2020-10-10 13:33
     * @功能说明:分类
     */
    public function getCateTextAttr($value,$data){

        if(!empty($data['cate_id'])){

            if(empty($this->cate_model)){

                $this->cate_model = new Cate();
            }

            $arr = $this->cate_model->where(['id'=>$data['cate_id']])->value('title');

            return $arr;

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-09 15:47
     * @功能说明:加料
     */
    public function getFeedingAttr($value,$data){

        if(!empty($data['id'])){

            $feeding_model = new Feeding();

            $arr = $feeding_model->where(['goods_id'=>$data['id']])->select()->toArray();

            return $arr;

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:26
     * @功能说明:
     */
    public function setImgsAttr($value){

        return !empty($value)?implode(',',$value):'';

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:26
     * @功能说明:
     */
    public function getImgsAttr($value){

        return !empty($value)?explode(',',$value):'';

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:商品添加
     */
    public function goodsAdd($data,$i=0){

        $data['create_time'] = time();

        $data['update_time'] = time();

        $data = $this->changeData($data);

        Db::startTrans();
        $spe       = [];

        $spe_price = [];

        $feeding   = [];

        //规格
        if(isset($data['spe'])){

            $spe = $data['spe'];

            unset($data['spe']);
        }
        //规格价格
        if(isset($data['spe_price'])){

            $spe_price = $data['spe_price'];

            unset($data['spe_price']);
        }
        //加料
        if(isset($data['feeding'])){

            $feeding = $data['feeding'];

            unset($data['feeding']);
        }

        $res = $this->insert($data);
        //菜品id
        $goods_id = $this->getLastInsID();

        $this->updateSome($spe,$spe_price,$goods_id,$data['uniacid'],$feeding,$i);

        Db::commit();
        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-10 11:04
     * @功能说明:
     */
    public function changeData($data){

        $data['imgs'] = !empty($data['imgs'])?implode(',',$data['imgs']):'';

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-09 15:12
     * @功能说明:
     */
    public function updateSome($spe,$spe_price,$goods_id,$uniacid,$feeding,$i=0){

        $spe_model = new Spe();

        $spe_price_model = new SpePrice();

        $feeding_model = new Feeding();
        //添加规格
        $arr = $spe_model->goodsSpeAdd($spe,$goods_id,$uniacid,$i);
        //添加规格价格
        $spe_price_model->goodsSpePriceAdd($spe_price,$goods_id,$arr,$uniacid);

        $feeding_model->dataAdd($feeding,$goods_id,$uniacid);

        return true;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:商品编辑
     */
    public function goodsUpdate($dis,$data){

        $data['update_time'] = time();

        $data = $this->changeData($data);

        $spe       = [];

        $spe_price = [];

        $feeding   = [];
        //规格
        if(isset($data['spe'])){

            $spe = $data['spe'];

            unset($data['spe']);
        }
        //规格价格
        if(isset($data['spe_price'])){

            $spe_price = $data['spe_price'];

            unset($data['spe_price']);
        }
        //加料
        if(isset($data['feeding'])){

            $feeding = $data['feeding'];

            unset($data['feeding']);
        }

        $res = $this->where($dis)->update($data);

        $this->updateSome($spe,$spe_price,$data['id'],$data['uniacid'],$feeding);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function goodsList($dis,$mapor=[],$page,$map){

//        dump($dis,$mapor=[],$page,$map);exit;

        $data = $this->where($dis)->where($map)
            ->where(function ($query) use ($mapor){
            $query->whereOr($mapor);
            })->order('top desc,id desc')
            ->paginate($page)
            ->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function goodsInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-16 10:35
     * @功能说明:小程序端端商品列表
     */
    public function indexGoodsList($dis){

        $data = $this->where($dis)->order('top desc')->paginate(20)->toArray();

        return $data;

    }

    /**
     * @param $goods_id
     * @功能说明:商品规格
     * @author chenniang
     * @DataTime: 2020-10-09 16:49
     */
    public function goodsSpeList($goods_id,$data){

        $spe_model = new Spe();

        $spe_price_model = new SpePrice();

        $dis['goods_id'] = $goods_id;

        $dis['status']   = 1;

        $data['spe']     = $spe_model->goodsSpe($dis);

        $data['spe_price']   = $spe_price_model->goodsSpePrice($dis);

        if(!empty($data['spe_price'])){

            foreach ($data['spe_price'] as &$v){

                $v['title']   = $v['spe_name'];

                $v['true_id'] = $v['id'];

                $v['id']      = implode(',',$v['spe_id']);

            }
        }
        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-12 16:26
     * @功能说明:同步商品
     */
    public function withGoods($goods_id,$store_id){

        if(!empty($goods_id)){

            foreach ($goods_id as $value){

                $goods = $this->goodsInfo(['id'=>$value]);

                $goods['top_id'] = $value;

                $goods['store_id'] = $store_id;

                unset($goods['id']);

                unset($goods['show_price']);

                unset($goods['total_stock']);

                unset($goods['show_member_price']);

                unset($goods['cate_text']);

                unset($goods['show_original_price']);

                unset($goods['show_sale_num']);

                $goods = $this->goodsSpeList($value,$goods);

                $have = $this->goodsInfo([['top_id','=',$value],['store_id','=',$store_id],['status','>',-1]]);

                if(empty($have)){

                    $this->goodsAdd($goods,1);
                }
            }
        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-16 11:36
     * @功能说明:购物车列表
     */
    public function carList($dis){

        $data = $this->alias('a')
                ->join('longbing_card_restaurant_car b','a.id = b.goods_id')
                ->where($dis)
                ->group('a.id')
                ->order('a.top')
                ->select()
                ->toArray();



    }




    /**
     * @author chenniang
     * @DataTime: 2020-10-16 15:57
     * @功能说明:计算购物车的价格
     */
    public function getStaffGoodsList($dis,$sort=1){

        switch ($sort){

            case 1:
                $incon = 'total_stock desc';
                break;
            case 2:
                $incon = 'total_stock';
                break;
            case 3:
                $incon = 'price desc';
                break;
            case 4:
                $incon = 'price';
                break;
        }

        $data = $this->alias('a')
                ->join('longbing_card_restaurant_spe_price b','b.goods_id = a.id')
                ->where($dis)
                ->field('a.*,SUM(b.stock) as total_stock,MIN(b.price) as price')
                ->group('a.id')
                ->order($incon)
                ->paginate(10)
                ->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-22 10:56
     * @功能说明:检查商品
     */
    public function checkGoods($user_id,$goods_id,$num=1){


        $dis = [

            'id'     => $goods_id,

            'status' => 1
        ];

        $goods = $this->goodsInfo($dis);

        if(empty($goods)){

            return ['code'=>500,'msg'=>'商品已经下架'];

        }

        if($goods['limit_status']==1){

            $dis = [

                'user_id' => $user_id,

                'goods_id'=> $goods_id,

            ];

            $car = new Car();
            //已经加入购物车到数量
            $have_num = $car->where($dis)->sum('num');

            if($goods['limit']<$have_num+$num){

                return ['code'=>500,'msg'=>'已达到购买上线'];

            }

        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-24 18:58
     * @功能说明:商品排行
     */
    public function goodsTop($store_id,$sort = 1,$start_time='',$end_time=''){

        $dis[] = ['a.status','>',-1];

        $dis[] = ['b.store_id','=',$store_id];

        $dis[] = ['b.pay_type','>',0];

        if(!empty($start_time)&&!empty($end_time)){

            $dis[] = ['b.create_time','between',"$start_time,$end_time"];
        }

        $sort = $sort==1?'pay_price desc':'goods_num desc';

        $data = $this->alias('a')
                ->join('longbing_card_restaurant_order_goods b','a.id= b.goods_id')
                ->where($dis)
                ->field('a.*,sum(b.pay_price) as pay_price,sum(b.goods_num) as goods_num')
                ->group('a.id')
                ->order($sort)
                ->paginate(10)
                ->toArray();

        return $data;

    }






}